package cn.wolfcode.controller;

import cn.wolfcode.anno.RequiredPermission;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.query.PageResult;
import cn.wolfcode.query.PermissionQueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;
    //处理所有请求
    //处理显示的请求
    @RequiredPermission(name="部门列表",expression = "department:list")
    @RequestMapping("/list")
    public String list(Model model,PermissionQueryObject q){
        PageInfo<Permission> pageInfo = permissionService.query(q);
        model.addAttribute("pageResult", pageInfo);
        return "permission/list";
    }
    //处理删除请求
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        permissionService.delete(id);
        return new JsonResult();
    }
    //处理去修改和保存的请求
    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Permission> permissions = permissionService.selectAll();
        model.addAttribute("permissions", permissions);
        if(id!=null){
            Permission permission = permissionService.selectOne(id);
            model.addAttribute("permission", permission);//回显
        }
        return "permission/input";
    }
    //处理修改和保存的请求
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission permission){
        if(permission.getId()!=null){
            permissionService.update(permission);
        }
        else{
            permissionService.insert(permission);
        }
        return "redirect:/permission/list.do";
    }
}